<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use App\Models\Squad;
use App\Models\Crouse;
use App\Models\Classroom;
use App\Models\Notice;
use App\Models\Crousesheet;
use App\Models\Term;


class Make_jiaxiao_notice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make_jiaxiao_notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //①查出课表中所有班级ID
        $squad_id = DB::table('crousesheets')->where('status','=',1)->select('squads_id')->distinct()->get();
        $squad_id = $this->objectToArray($squad_id);
        for ($i=0; $i < count($squad_id); $i++) { 
            //查出各班级每周的第一节课ID
            $crousesheets_id = Crousesheet::where('squads_id',$squad_id[$i]['squads_id'])->where('status','=','1')->orderBy('section','ASC')->orderBy('start_time','ASC')->first();
            //得出当前是第几周
            
            $crousesheet_id = $crousesheets_id['id'];
            $squad_name = Squad::where('id',$crousesheets_id['squads_id'])->value('name');
            switch ($crousesheets_id['section']) {
                case '1':
                    $section_name = '周一';
                    $date = date('Y-m-d H:i:s');
                    break;
                case '2':
                    $section_name = '周二';
                    $date = date('Y-m-d H:i:s',strtotime('+1 day'));
                    break;
                case '3':
                    $section_name = '周三';
                    $date = date('Y-m-d H:i:s',strtotime('+2 day'));
                    break;
                case '4':
                    $section_name = '周四';
                    $date = date('Y-m-d H:i:s',strtotime('+3 day'));
                    break;
                case '5':
                    $section_name = '周五';
                    $date = date('Y-m-d H:i:s',strtotime('+4 day'));
                    break;
                case '6':
                    $section_name = '周六';
                    $date = date('Y-m-d H:i:s',strtotime('+5 day'));
                    break;
                case '7':
                    $section_name = '周日';
                    $date = date('Y-m-d H:i:s',strtotime('+6 day'));
                    break;
                default:
                    $section_name = 'N/A';
                    $date = 'N/A';
                    break;
            }
            $start_time = $crousesheets_id['start_time'];
            $end_time = $crousesheets_id['end_time'];
            $classroom_name = Classroom::where('id',$crousesheets_id['classroom_id'])->value('name');
            $crouse_name = Crouse::where('id',$crousesheets_id['crouse_id'])->value('name');
            $squads_id = $crousesheets_id['squads_id'];
            $tutor = $crousesheets_id['tutor_id'];
            $term_start_date = Term::where('status','0')->value('start_date');
            $zhou = ceil((strtotime(date('Y-m-d'))-strtotime($term_start_date))/3600/24/7);
            $jiaxiao_data[] = [
                'notice_type' =>'3',
                'crousesheet_id' =>$crousesheet_id,
                'event' =>'录入家校【第'.$zhou.'周】！(班级：'.$squad_name.'  时间：'.date('Y-m-d',strtotime($date)).' '.$section_name.'  '.$start_time.'-'.$end_time.' 教室：'.$classroom_name.' 课程：'.$crouse_name.')',
                'starttime' =>$date,
                'grace_days'=>'7',
                'endtime' =>date('Y-m-d H:i:s',strtotime('+7 day',strtotime($date))),
                'status'=>'1',
                'squad_id'=>$squads_id,
                'tutor_id'=>$tutor
            ];
            // $zuoye_data[] = [
            //     'notice_type' =>'5',
            //     'crousesheet_id' =>$crousesheet_id,
            //     'event' =>'录入作业【第'.$zhou.'周】！(班级：'.$squad_name.'  时间：'.date('Y-m-d',strtotime($date)).' '.$section_name.'  '.$start_time.'-'.$end_time.' 教室：'.$classroom_name.' 课程：'.$crouse_name.')',
            //     'starttime' =>$date,
            //     'grace_days'=>'7',
            //     'endtime' =>date('Y-m-d H:i:s',strtotime('+7 day',strtotime($date))),
            //     'status'=>'1',
            //     'squad_id'=>$squads_id,
            //     'tutor_id'=>$tutor
            // ];
        }
        $jiaxiao_res = Notice::insert($jiaxiao_data);
        //$zuoye_res = Notice::insert($zuoye_data);
    }

    function objectToArray($object) {
        //先编码成json字符串，再解码成数组
        return json_decode(json_encode($object), true);
    }
}
